﻿using FMStudio.Documents.TemplateDocument.Properties;
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.Xml.Serialization;

namespace FMStudio.Documents.TemplateDocument.Models
{
    public class Folder : Item
    {
        #region Fields
        private string name;
        private string targetFolderName;
        private readonly ObservableCollection<Item> items;
        #endregion


        public Folder(string name)
        {
            if (name == null) { throw new ArgumentNullException("name"); }

            this.name = name;
            this.targetFolderName = name;

            this.items = new ObservableCollection<Item>();
            AddWeakEventListener(this.items, ItemsCollectionChanged);
        }

        public Folder()
            : this(string.Empty)
        {
        }


        #region Properties
        /// <summary>
        /// Gets or sets the name of the folder.
        /// </summary>
        [XmlAttribute()]
        [Required(
            ErrorMessageResourceName = "FolderNameMandatory", 
            ErrorMessageResourceType = typeof(Resources))]
        public string Name
        {
            get { return this.name; }
            set
            {
                if (this.name != value)
                {
                    this.name = value;
                    RaisePropertyChanged("Name");
                }
            }
        }

        /// <summary>
        /// Gets or sets the name of the target folder.
        /// </summary>
        [XmlAttribute()]
        [Required(
            ErrorMessageResourceName = "FolderTargetFolderNameMandatory",
            ErrorMessageResourceType = typeof(Resources))]
        public string TargetFolderName
        {
            get { return this.targetFolderName; }
            set
            {
                if (this.targetFolderName != value)
                {
                    this.targetFolderName = value;
                    RaisePropertyChanged("TargetFolderName");
                }
            }
        }

        /// <summary>
        /// The items contains in the folder.
        /// </summary>
        [XmlElement(Type = typeof(Folder)),
         XmlElement(Type = typeof(ProjectItem))]
        public ObservableCollection<Item> Items { get { return this.items; } }
        #endregion

        #region Methods
        #region Private Methods
        private void ItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            RaisePropertyChanged("Items");
        }
        #endregion
        #endregion
    }
}
